import axios from 'axios'
import { ElMessage } from 'element-plus'

const instance = axios.create({
  // baseURL: '', // 如有后端代理可配置
  timeout: 10000
})

instance.interceptors.response.use(
  response => response,
  error => {
    if (error.response) {
      ElMessage.error(error.response.data || '请求失败')
      if (error.response.status === 401) {
        localStorage.removeItem('userId')
        localStorage.removeItem('username')
        window.location.href = '/login'
      }
    } else {
      ElMessage.error('网络异常')
    }
    return Promise.reject(error)
  }
)

export default instance